;(function($) {
    $.fn.overlay = function() {
        var overlay = $(this);
        if($.ua.isIe6 || $.ua.isIe7 || $.ua.isIe8 || $.ua.isQQ || $.ua.isLiebao){
            overlay.css("zIndex","1100");
            if(overlay.hasClass("open")){
                overlay.removeClass("open").addClass("close");
            }else{
                overlay.removeClass("close").addClass("open");
                if(!overlay.find(".overlay-mark").get(0)){
                    var div = $("<div></div>");
                    div.css({
                        background: "#000",
                        width: "100%",
                        height: "100%",
                        position: "absolute",
                        left: "0px",
                        top: "0px",
                        zIndex: "-1",
                        opacity: "0.5",
                        filter: "Alpha(opacity=50)"
                    });
                    div.attr("class","overlay-mark");
                    overlay.append(div);
                }
            }

            return;
        }else{
            if(overlay.length > 0){
                transEndEventNames = {
                        'WebkitTransition': 'webkitTransitionEnd',
                        'MozTransition': 'transitionend',
                        'OTransition': 'oTransitionEnd',
                        'msTransition': 'MSTransitionEnd',
                        'transition': 'transitionend'
                    },
                    transEndEventName = transEndEventNames[Modernizr.prefixed('transition')],
                    support = {
                        transitions: Modernizr.csstransitions
                    };
                s = Snap(overlay.find('svg').get(0)),
                    path = s.select('path'),
                    steps = overlay.attr('data-steps').split(';'),
                    stepsTotal = steps.length;

                function toggleOverlay() {
                    if (overlay.hasClass("open")) {
                        var pos = stepsTotal - 1;
                        overlay.removeClass("open").addClass("close");

                        var onEndTransitionFn = function(ev) {
                                overlay.removeClass("close");
                            },
                            nextStep = function(pos) {
                                pos--;
                                if (pos < 0) return;
                                path.animate({
                                    'path': steps[pos]
                                }, 60, mina.linear, function() {
                                    if (pos === 0) {
                                        onEndTransitionFn();
                                    }
                                    nextStep(pos);
                                });
                            };

                        nextStep(pos);
                    } else if (!overlay.hasClass("close")) {
                        var pos = 0;
                        overlay.addClass("open");

                        var nextStep = function(pos) {
                            pos++;
                            if (pos > stepsTotal - 1) return;
                            path.animate({
                                'path': steps[pos]
                            }, 60, mina.linear, function() {
                                nextStep(pos);
                            });
                        };

                        nextStep(pos);
                    }
                }

                toggleOverlay();
            }else{
                return ;
            }
        }
    };

})(jQuery);
